package com.htht.smartschool.util.system;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.htht.smartschool.model.bigexam.BigExamBean;
 
public class ResultSetToExcel {
    /**
     * 写Excel操作
     * 
     * @param fileName
     * @param coloumItems
     * @param sql
     */
	public static String savepath;
	private String coloumItem = "";
    private String msg = "";
    private List<Object[]> rs;
    private boolean flag;
    private List<BigExamBean> exam;
    public String getColoumItem() {
		return coloumItem;
	}
	public void setColoumItem(String coloumItem) {
		this.coloumItem = coloumItem;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public List<BigExamBean> getExam() {
		return exam;
	}
	public void setExam(List<BigExamBean> exam) {
		this.exam = exam;
	}
	public List<Object[]> getRs() {
		return rs;
	}
	public void setRs(List<Object[]> rs) {
		this.rs = rs;
	}
	public boolean getFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
 
    @SuppressWarnings("resource")
	public void writeExcel() {
    	String fileName = UUID.randomUUID().toString();
    	String backupPath = savepath;
        File backupFolder = new File(backupPath);
        if (!backupFolder.exists()) {
            backupFolder.mkdir();
        } 
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(backupPath  +"/"+ fileName
                    + ".xls");
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();
            sheet.autoSizeColumn(1);
            HSSFRow row = null;
            HSSFCell cell = null;
            // 写表格的列名
            row = sheet.createRow(0);
            String[] coloumItems = coloumItem.split(",");
            for (int i = 0; i < coloumItems.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(coloumItems[i]);
                sheet.autoSizeColumn(i);
            }
            for (int j = 1; j <= rs.size(); j++) {
            	Object[]  values = rs.get(j-1);
            	row = sheet.createRow(j);
            	for (int i = 1; i <= values.length; i++) {
            		cell = row.createCell(i - 1);
                    Object obj = values[i-1];
                    cell.setCellValue(obj + "");
				}
                
            }
//            for (int i = 0; i < coloumItems.length; i++) {
//            	if(i == 4){
//            		System.out.println(i);	
//            	}
//            	int maxColumnWidth = sheet.getColumnWidth(i);
//            	int width = 0;
//            	for (int j = 1; j <= rs.size(); j++) {
//            		if(j==41){
//            			System.out.println("j"+j);
//            		}
//            		if(sheet.getRow(j).getCell(i).getStringCellValue().length() != 0){
//            			int w = sheet.getRow(j).getCell(i).getStringCellValue().length() * 512;
//            			if(w >width){
//            				width = w; 
//            			}
//            		}
//            	}
//            	maxColumnWidth = (maxColumnWidth>width) ? maxColumnWidth : width;
//            	sheet.setColumnWidth(i, maxColumnWidth+512);
//           }
           workbook.write(fileOutputStream);
           fileOutputStream.close();
           msg = ConfigUtil.getProperty("downloads","url") + fileName+ ".xls";
//           msg =fileName+ ".xls";
           setFlag(true);
        } catch (FileNotFoundException e) {
        	setFlag(false);
        	msg = e.getMessage();
        } catch (IOException e) {
        	setFlag(false);
        	msg = e.getMessage();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                	setFlag(false);
                	msg = e.getMessage();
                }
            }
        }
    }
    
}